Get the 2 largest and 3 smallest items from datasetΒΆ
heapq.nlargest(2, H) heapq.nsmallest(3, H)
Get the two largest and three smallest items from a dataset.
Expected output:
[100, 90]
[10, 20, 20]
import heapq
H = [10, 20, 50, 70, 90, 20, 50, 40, 60, 80, 100]
print(heapq.nlargest(2, H))
print(heapq.nsmallest(3, H))
Output:
[100, 90]
[10, 20, 20]